/* Comments */
Config.maxThreadDepth = 4;
var Cur_Cform_Hdr = '.comment_form';
var Cur_Cform_Url = $('#comment-editor').attr('src');
function trim(str) {
    var whitespace = ' \n\r\t\f\x5b\x5d\x7c\x7d\x3c\x3e\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
    for (var i = 0; i < str.length; i++) {
        if (whitespace.indexOf(str.charAt(i)) != -1) {
            str = str.substring(0, i);
            break
        }
    }
    return str
}
$('#comment_block .comment_body p').html(function (index, oldhtml) {
    if (Replace_Youtube_Link) {
        var search_key = 'http://www.youtube.com/watch?v=';
        var check_index = oldhtml.indexOf(search_key);
        while (check_index != -1) {
            ht = oldhtml.substring(check_index);
            yt_link = trim(ht);
            var yt_code_index = yt_link.indexOf('&');
            var yt_code = '';
            if (yt_code_index == -1) {
                yt_code = yt_link.substring(search_key.length)
            } else {
                yt_code = yt_link.substring(search_key.length, yt_code_index)
            }
            var yt_video = '<iframe class="comment_youtube" src="http://www.youtube.com/embed/' + yt_code + '?autohide=1" frameborder="0" allowfullscreen></iframe>';
            oldhtml = oldhtml.substring(0, check_index) + yt_video + oldhtml.substring(check_index + yt_link.length);
            check_index = oldhtml.indexOf(search_key);
            if (check_index == -1) {
                search_key = 'https://www.youtube.com/watch?v=';
                check_index = oldhtml.indexOf(search_key)
            }
        }
    }
    if (Replace_Image_Link) {
        var save_html = '';
        var temp_html = oldhtml;
        for (var i = 0; i < Replace_Image_Ext.length; i++) {
            var search_key = '.' + Replace_Image_Ext[i];
            var upper_html = temp_html.toUpperCase();
            var check_index = upper_html.indexOf(search_key);
            while (check_index != -1) {
                img_src = temp_html.substring(0, check_index + search_key.length);
                upper_html = img_src.toUpperCase();
                var http_search = 'HTTP://';
                var find_http = upper_html.indexOf(http_search);
                var save_http = '';
                while (find_http != -1) {
                    save_http = http_search.toLowerCase();
                    img_src = img_src.substring(find_http + http_search.length);
                    upper_html = img_src.toUpperCase();
                    find_http = upper_html.indexOf(http_search)
                }
                http_search = 'HTTPS://';
                upper_html = img_src.toUpperCase();
                find_http = upper_html.indexOf(http_search);
                while (find_http != -1) {
                    save_http = http_search.toLowerCase();
                    img_src = img_src.substring(find_http + http_search.length);
                    upper_html = img_src.toUpperCase();
                    find_http = upper_html.indexOf(http_search)
                }
                if (save_http == '' || img_src.length < 6) {
                    break
                }
                img_src = save_http + img_src;
                save_html += temp_html.substring(0, check_index + search_key.length - img_src.length) + '<img src="' + img_src + '" class="comment_img"/>';
                temp_html = temp_html.substring(check_index + search_key.length);
                upper_html = temp_html.toUpperCase();
                check_index = upper_html.indexOf(search_key)
            }
        }
        oldhtml = save_html + temp_html
    }
    if (Display_Emo) {
        var length = Emo_List.length;
        if (length % 2 == 1) {
            length--
        }
        for (var i = 0; i < length; i += 2) {
            var img_html = '<img src="' + Emo_List[i + 1] + '" class="comment_emo"/>';
            check_index = oldhtml.indexOf(Emo_List[i]);
            while (check_index != -1) {
                oldhtml = oldhtml.substring(0, check_index) + img_html + oldhtml.substring(check_index + Emo_List[i].length);
                check_index = oldhtml.indexOf(Emo_List[i])
            }
        }
    }
    if (Replace_Force_Tag) {
        var length = Force_Tag.length;
        if (length % 2 == 1) {
            length--
        }
        for (var i = 0; i < length; i += 2) {
            while (1) {
                var temp_html = oldhtml.toLowerCase();
                check_index = temp_html.indexOf(Force_Tag[i]);
                if (check_index != -1) {
                    oldhtml = oldhtml.substring(0, check_index) + Force_Tag[i + 1] + oldhtml.substring(check_index + Force_Tag[i].length)
                } else {
                    break
                }
            }
        }
    }
    return oldhtml
});
$('.comment_emo_list').html(function (index, oldhtml) {
    if (Display_Emo) {
        var length = Emo_List.length;
        if (length % 2 == 1) {
            length--
        }
        var newhtml = '';
        for (var i = 0; i < length; i += 2) {
            var img_code = '<span>' + Emo_List[i] + '</span>';
            var img_html = '<img src="' + Emo_List[i + 1] + '" class="comment_emo"/>';
            newhtml += '<div class="item">' + img_html + img_code + '</div>'
        }
        return newhtml
    }
});
$('.comment_wrap .comment_body p').html(function (i, h) {
    temp = h.toLowerCase();
    index = temp.indexOf('@<a href="#c');
    if (index != -1) {
        index_tail = temp.indexOf('</a>', index);
        if (index_tail != -1) {
            h = h.substring(0, index) + h.substring(index_tail + 4)
        }
    }
    return h
});

function Valid_Par_Id(par_id) {
    r = par_id.indexOf('c');
    if (r != -1) par_id = par_id.substring(r + 1);
    return par_id
}

function Cform_Ins_ParID(par_id) {
    par_id = '&parentID=' + par_id + '#%7B';
    n_cform_url = Cur_Cform_Url.replace(/#%7B/, par_id);
    return n_cform_url
}

function Reset_Comment_Form() {
    html = $(Cur_Cform_Hdr).html();
    $(Cur_Cform_Hdr).html('');
    Cur_Cform_Hdr = '.comment_form';
    $(Cur_Cform_Hdr).html(html);
    $('#comment-editor').attr('src', Cur_Cform_Url)
}

function Display_Reply_Form(e) {
    par_id = $(e).attr('id');
    par_id = Valid_Par_Id(par_id);
    html = $(Cur_Cform_Hdr).html();
    if (Cur_Cform_Hdr == '.comment_form') {
        reset_html = '<a href="javascript:void(0);" onclick="Reset_Comment_Form()" style="display:none">' + Msgs.addComment + '</a><a name="origin_cform"/>';
        $(Cur_Cform_Hdr).html(reset_html)
    } else {
        $(Cur_Cform_Hdr).html('')
    }
    Cur_Cform_Hdr = '#r_f_c' + par_id;
    $(Cur_Cform_Hdr).html(html);
    $('#comment-editor').attr('src', Cform_Ins_ParID(par_id))
}
cur_url = window.location.href;
search_formid = '#comment-form_';
search_index = cur_url.indexOf(search_formid);
if (search_index != -1) {
    ret_id = cur_url.substring(search_index + search_formid.length);
    Display_Reply_Form('#rc' + ret_id)
}
for (var i = 0; i < Items.length; i++) {
    if ('parentId' in Items[i]) {
        var par_id = Items[i].parentId;
        var par_level = parseInt($('#c' + par_id + ':first').attr('level'));
        $('#c' + par_id + ' .comment_child:first').html(function (index, oldhtml) {
            var child_id = Items[i].id;
            if (par_level >= Config.maxThreadDepth) {
                $('#c' + child_id + ':first .comment_reply').remove()
            }
            var child_html = $('#c' + child_id + ':first').html();
            child_html = '<div class="comment_wrap" id="c' + child_id + '" level="' + (par_level + 1) + '">' + child_html + '</div>';
            $('#c' + child_id).remove();
            return (oldhtml + child_html)
        })
    }
}

$('#comments').on("click", "#cancel-answer", function () {
    $('.comment_form').appendTo('#comments');
    $('#comment-editor').css('opacity', 0).attr('src', ao[0] + '#' + ao[1]).on("load", function () {
        $(this).css('opacity', 1);
        al.html('');
    });
});

/* Comments Manifulation */

function repText(id) {
    var a = (document.getElementById(id)) ? document.getElementById(id) : "",
        b = (a !== "") ? a.innerHTML : a,
        b = b.replace(/<i rel="image">(.*?)<\/i>/ig, "<img class='cm-image' src='$1' alt='loading...' \/>");
    b = b.replace(/\[img\](.*?)\[\/img\]/ig, "<img class='cm-image' src='$1' alt='loading...' \/>");
    b = b.replace(/<i rel="youtube">http:\/\/www\.youtube\.com\/embed\/(.*?)<\/i>/ig, "<iframe class='cm-youtube' src='http://www.youtube.com/embed/$1'><\/iframe>");
    b = b.replace(/<i rel="youtube">(http:\/\/youtu\.be\/|http:\/\/www\.youtube\.com\/watch\?v\=)(.*?)<\/i>/ig, "<iframe class='cm-youtube' src='http://www.youtube.com/embed/$2'><\/iframe>");
    b = b.replace(/\[youtube\]http:\/\/www\.youtube\.com\/embed\/(.*?)\[\/youtube\]/ig, "<iframe class='cm-youtube' src='http://www.youtube.com/embed/$1'><\/iframe>");
    b = b.replace(/\[youtube\](http:\/\/youtu\.be\/|http:\/\/www\.youtube\.com\/watch\?v\=)(.*?)\[\/youtube\]/ig, "<iframe class='cm-youtube' src='http://www.youtube.com/embed/$2'><\/iframe>");
    b = b.replace(/<i rel="code">(.*?)<\/i>/ig, "<code>$1<\/code>");
    b = b.replace(/<i rel="pre">(.*?)<\/i>/ig, "<pre><code>$1<\/code><\/pre>");
    b = b.replace(/<b rel="quote">(.*?)<\/b>/ig, "<bquote>$1<\/bquote>");
    b = b.replace(/\[code\](.*?)\[\/code\]/ig, "<code>$1<\/code>");
    b = b.replace(/\[pre\](.*?)\[\/pre\]/ig, "<pre>$1<\/pre>");
    b = b.replace(/&amp;feature=[0-9a-zA-Z-_]*/ig, "");
    b = b.replace(/\/s(640|1600)/g, "/s400");
    if (document.getElementById(id)) {
        document.getElementById(id).innerHTML = b;
    }
}
repText('cm_holder');

/**
 * Simple regex experiment to create an automatic emoticons by Dharla Ferdana
 * Visit: http://www.dhf.web.id
 * Update: 23 August 2012
 */
$(function () {

    // Append an emoticon bar before comment-form
    if (putEmoAbove) {
        $(putEmoAbove).before('<div class="heads-up" id="emoWrap"> :) :( ^_^ :D ;) :-bd :yaya: :&#39;( :&#92; :p B) :Q :Ozz 7:( &#92;o/ **p &lt;3 0:) :-a 7:O *fck* x@ X@ ~x( =p*  </div>');
    }

    function emo(emo, imgRep, emoKey) {
        $(emoRange).each(function () {
            $(this).html($(this).html().replace(/<br ?\/?>(:|;|=|\^)/ig, "<br> $1").replace(emo, " <img src='" + imgRep + "' class='emo delayLoad' alt='" + emoKey + "' />"));
        });
    }
	emo(/\s:\)+/g, "http://reader-hosting.googlecode.com/svn/branches/emotion/smile.gif", ":)");
	emo(/\s;\)+/g, "http://reader-hosting.googlecode.com/svn/branches/emotion/wink.gif", ";)");
	emo(/\s:\(/g, "http://reader-hosting.googlecode.com/svn/branches/emotion/sad.gif", ":(");
	emo(/\s:yaya:/ig, "http://reader-hosting.googlecode.com/svn/branches/emotion/yaya.gif", ":yaya:");
	emo(/\s:\\/g, "http://reader-hosting.googlecode.com/svn/branches/emotion/memble.gif", ":&#92;");
	emo(/\s:D/g, "http://reader-hosting.googlecode.com/svn/branches/emotion/haha.gif", ":D");
	emo(/\s\^(\_|)\^/g, "http://reader-hosting.googlecode.com/svn/branches/emotion/senyum-tulus.gif", "^_^");
	emo(/\s:'\(/g, "http://reader-hosting.googlecode.com/svn/branches/emotion/cry.gif", ":&#39;(");
	emo(/\sB\)/g, "http://reader-hosting.googlecode.com/svn/branches/emotion/cool.gif", "B)");
	emo(/\s:Q/ig, "http://reader-hosting.googlecode.com/svn/branches/emotion/smoking.gif", ":Q");
	emo(/\s\*\*p/ig, "http://reader-hosting.googlecode.com/svn/branches/emotion/crazy.gif", "**p");
	emo(/\s7:\(/g, "http://reader-hosting.googlecode.com/svn/branches/emotion/conf.gif", "7:(");
	emo(/\s:p/ig, "http://reader-hosting.googlecode.com/svn/branches/emotion/wee.gif", ":p");
	emo(/\s:Oz+/ig, "http://reader-hosting.googlecode.com/svn/branches/emotion/sleep.gif", ":Ozz");
	emo(/\s7:O/ig, "http://reader-hosting.googlecode.com/svn/branches/emotion/angry.gif", "7:O");
	emo(/\s\\o\//ig, "http://reader-hosting.googlecode.com/svn/branches/emotion/applause.gif", "&#92;o/");
	emo(/\s&lt;3/ig, "http://reader-hosting.googlecode.com/svn/branches/emotion/love.gif", "&amp;amp;lt;3");
	emo(/\s0:\)+/ig, "http://reader-hosting.googlecode.com/svn/branches/emotion/angelgreen.gif", "0:)");
	emo(/\s:-a/ig, "http://reader-hosting.googlecode.com/svn/branches/emotion/vomit.gif", ":-a");
	emo(/\s\*fck\*/ig, "http://reader-hosting.googlecode.com/svn/branches/emotion/fuck.gif", "*fck*");
	emo(/\sx\@/g, "http://reader-hosting.googlecode.com/svn/branches/emotion/marahbesar.gif", "x@");
	emo(/\s\X\@/g, "http://reader-hosting.googlecode.com/svn/branches/emotion/arrgh.gif", "X@");
	emo(/\s:-bd/ig, "http://reader-hosting.googlecode.com/svn/branches/emotion/topmarkotop.gif", ":-bd");
	emo(/\s\~x\(+/ig, "http://reader-hosting.googlecode.com/svn/branches/emotion/ugh.gif", "~x(");
	emo(/\s=p\*/ig, "http://reader-hosting.googlecode.com/svn/branches/emotion/ball2.gif", "=p*");

    var one = 0; // Show alert one times!

    // Click anywhere to hide the emoticon
    $(document.body).on("click", function () {
        $('.emoKey').remove();
    });

    // Click to show the code!
    $('.emo').css('cursor', 'pointer').on("click", function(e) {
    $('.emoKey').remove();
    $(this).after('<input class="emoKey" type="text" size="6" value=" ' + this.alt + '" />');
    $('.emoKey').trigger("select");
    if (emoMessage && one === 0) {
        // Tampilkan kotak dialog di sini
        $('#kotak-dialog').fadeIn(400).html(emoMessage);
        one = 1;
    }
    e.stopPropagation();
});

});


